//
//  DataProvider.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/13.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "DataProvider.h"
#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"
#import "AFURLRequestSerialization.h"
#import "SVProgressHUD.h"


@implementation DataProvider

#pragma mark - 登陆
- (void)loginWithUsername:(NSString *)username password:(NSString *)password
{
    if(username && password)
    {
        NSString * url = [NSString stringWithFormat:@"%@",Url];
        NSDictionary * prm = @{@"data":[NSString stringWithFormat:@"{\"username\":\"%@\",\"password\":\"%@\",\"method\":\"verify\"}",username,password]};
        
        [self PostRequest:url andpram:prm];
    }
}


#pragma mark - 用户基本信息请求
- (void)userInfoWithUid:(NSString *)uid token:(NSString *)token
{
    if(uid && token)
    {
        NSString * url = [NSString stringWithFormat:@"%@",Url];
        NSDictionary * prm = @{@"data":[NSString stringWithFormat:@"{\"uid\":\"%@\",\"token\":\"%@\",\"method\":\"user_info\"}",uid,token]};
        
        [self PostRequest:url andpram:prm];
    }
}

#pragma mark - 站点服务列表//包含 站点 和 seo 通用
- (void)webListWithUid:(NSString *)uid token:(NSString *)token
{
    if(uid && token)
    {
        NSString * url = [NSString stringWithFormat:@"%@",Url];
        NSDictionary * prm = @{@"data":[NSString stringWithFormat:@"{\"uid\":\"%@\",\"token\":\"%@\",\"method\":\"seo_service_list\"}",uid,token]};
        
        [self PostRequest:url andpram:prm];
    }
}


#pragma mark - 根据站点sid获取关键词列表
- (void)keywordListWithUid:(NSString *)uid token:(NSString *)token sid:(NSString *)sid
{
    if(uid && token && sid)
    {
        NSString * url = [NSString stringWithFormat:@"%@",Url];
        NSDictionary * prm = @{@"data":[NSString stringWithFormat:@"{\"uid\":\"%@\",\"token\":\"%@\",\"sid\":\"%@\",\"method\":\"seo_keyword_list\"}",uid,token,sid]};
        
        [self PostRequest:url andpram:prm];
    }
}


























































#pragma mark 赋值回调
- (void)setDelegateObject:(id)cbobject setBackFunctionName:(NSString *)selectorName
{
    CallBackObject = cbobject;
    callBackFunctionName = selectorName;
}


-(void)PostRequest:(NSString *)url andpram:(NSDictionary *)pram
{
    AFHTTPRequestOperationManager * manage=[[AFHTTPRequestOperationManager alloc] init];
    manage.responseSerializer=[AFHTTPResponseSerializer serializer];
    manage.requestSerializer=[AFHTTPRequestSerializer serializer];
    manage.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/json",@"application/json", @"text/html",@"text/javascript", nil];//可接收到的数据类型
    manage.requestSerializer.timeoutInterval=20;//设置请求时限
    NSDictionary * prm =[[NSDictionary alloc] init];
    if (pram!=nil) {
        prm=pram;
    }
    [manage POST:url parameters:prm success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //        NSDictionary * dict =responseObject;
        NSString *str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        //        /*解析xml字符串开始*/
        //        SBXMLParser * parser = [[SBXMLParser alloc] init];
        //        XMLElement * root = [parser parserXML:[str dataUsingEncoding:NSASCIIStringEncoding]];
        //        NSLog(@"解析后：root=%@",root.text);
        //        /*解析xml字符串结束*/
//        NSLog(@"check time 1");
        NSData * data =[str dataUsingEncoding:NSUTF8StringEncoding];
        id dict =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        SEL func_selector = NSSelectorFromString(callBackFunctionName);
        if ([CallBackObject respondsToSelector:func_selector]) {
            NSLog(@"回调成功...");
            [CallBackObject performSelector:func_selector withObject:dict];
        }else{
            NSLog(@"回调失败...");
            [SVProgressHUD dismiss];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
        
        [SVProgressHUD showErrorWithStatus:@"请检查网络"];
//        [SVProgressHUD showErrorWithStatus:@"请检查网络" maskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD dismiss];
    }];
}




-(void)GetRequest:(NSString *)url andpram:(NSDictionary *)pram
{
    AFHTTPRequestOperationManager * manage=[[AFHTTPRequestOperationManager alloc] init];
    manage.responseSerializer=[AFHTTPResponseSerializer serializer];
    manage.requestSerializer=[AFHTTPRequestSerializer serializer];
    manage.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/json",@"application/json", @"text/html",@"text/javascript", nil];//可接收到的数据类型    manage.requestSerializer.timeoutInterval=10;//设置请求时限
    NSDictionary * prm =[[NSDictionary alloc] init];
    if (pram!=nil) {
        prm=pram;
    }
    [manage GET:url parameters:prm success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        NSData * data =[str dataUsingEncoding:NSUTF8StringEncoding];
        
        id dict =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        SEL func_selector = NSSelectorFromString(callBackFunctionName);
        if ([CallBackObject respondsToSelector:func_selector]) {
            NSLog(@"回调成功...");
            [CallBackObject performSelector:func_selector withObject:dict];
        }else{
            NSLog(@"回调失败...");
            [SVProgressHUD dismiss];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
        
        [SVProgressHUD showErrorWithStatus:@"请检查网络"];
//        [SVProgressHUD showErrorWithStatus:@"请检查网络" maskType:SVProgressHUDMaskTypeBlack];
    }];
    
}

// 将字典或者数组转化为JSON串
- (NSData *)toJSONData:(id)theData
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    if ([jsonData length] > 0 && error == nil)
    {
        return jsonData;
    }
    else
    {
        return nil;
    }
}


@end
